<?php
namespace Util;
/**
 * Created by PhpStorm.
 * User: mao
 * Date: 2016/8/9
 * Time: 14:41
 */

class  WechatSendMsg {
    private $APPID = "wx054896dce50a2918";
    private $APPSECRET = "60532758cd2cc25c41eb1b0642f4d043";
    private $access_token = '';

    public function __construct($APPID = NULL, $APPSECRET = NULL) {
        $wt = new \Util\WechatTicket();
        $this->APPID = $APPID;
        $this->APPSECRET = $APPSECRET;
        $wt->set($APPID, $APPSECRET);
        $this->access_token = $wt->getAccessToken();
    }


    public function SendTplMsg($data) {
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->access_token;
        $res = $this->http_request($url, $data);
        return json_decode($res, true);
    }


    private function http_request($url, $data = NULL) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
